package data;

import java.util.ArrayList;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;

public class ClusteredNode implements Comparable<ClusteredNode>{
	
	private SortedSet<Gene> genes;
	private SortedSet<ClusteredNode> clusters;
	
	private double[] veriablesAverage;
	private double clusterAverage;

	public ClusteredNode() {		
		genes = new TreeSet<Gene>() ;
		clusters = new TreeSet<ClusteredNode>();
	}
	
	public void add(Gene gene) {
		genes.add(gene);
		calculateAverages();
	}
		
	public void add(ClusteredNode cluster) {
		clusters.add(cluster);
		calculateAverages();
	}
		
	@Override
	public int compareTo(ClusteredNode c) {
		return new Double(clusterAverage).compareTo(new Double(c.clusterAverage));
	}

	public SortedSet<Gene> getGenes() {
		SortedSet<Gene> list = new TreeSet<Gene>();
		list.addAll(genes);
		for (ClusteredNode cluster:clusters) {
			list.addAll(cluster.getGenes());
		}
		return list;
	}
	
	public List<ClusteredNode> getClusters() {
		List<ClusteredNode> list = new ArrayList<ClusteredNode>(clusters.size());
		if (clusters.size()==0) {
			list.add(this);
			return list;
		}
		list.addAll(clusters);
		return list;
	}

	public String toString() {
		String str="";

		for (Gene g : getGenes()) {
			str += g.toString();
		}
		
		return str;
	}
	
	public double[] getVeriablesAverage() {
		return veriablesAverage;
	}
	
	public double getClusterAverage() {
		return clusterAverage;
	}
	
	private void calculateAverages() {
		clusterAverage=0;
		SortedSet<Gene> gs = getGenes();
		for (Gene g:gs) {
			clusterAverage += g.getAverage();
		}
		clusterAverage = clusterAverage/gs.size();
	}
}
